//页面加载的时候 计算合计
function sumPrice(){
	var sum = 0;
	var proMealPrice = $(".proMealPrice");
	
	for(var i=0;i<proMealPrice.length;i++){
		sum = Number(sum) + Number(parseFloat(proMealPrice[i].innerHTML).toFixed(2));
	}
	$('#sumPrice').html(parseFloat(sum).toFixed(2));
	$('#finallySumPrice').html(parseFloat(sum).toFixed(2));
}

//核对订单 
function submitOrders(){
	var shoppingCartId = $("input[name='shoppingCartId']");
	var proMealId = $("input[name='proMealId']");
	var proMealName = $("input[name='proMealName']");
	var useLife = $("input[name='useLife']");
	var userNum = $("input[name='userNum']");
	var proMealPrice = $(".proMealPrice");
	var proMealNum = $(".proMealNum");
	var version = $("input[name='version']");
	var pmStatus = $("input[name='pmStatus']");
	var userId = $("input[name='userId']");
	var isFree = $("input[name='isFree']");
	var productId = $("input[name='productId']").val();
	var endTime = $("input[name='endTime']");
	
	var orderList = [];
	var sumPrice = '0.0';
	for(var i=0;i<shoppingCartId.length;i++){
		var olist = {
			shoppingCartId:shoppingCartId[i].value,
			proMealId:proMealId[i].value,
			proMealName:proMealName[i].value,
			useLife:useLife[i].value,
			userNum:userNum[i].value,
			proMealPrice:proMealPrice[i].innerHTML,
			proMealNum:proMealNum[i].innerHTML,
			version:version[i].value,
			pmStatus:pmStatus[i].value,
			userId:userId[i].value,
			isFree:isFree[i].value,
			endTime:endTime[i].value 
		}
		sumPrice += Number(proMealPrice[i].innerHTML);
		orderList.push(olist);
	}
	orderList = JSON.stringify(orderList);
	var isPay;
	if(Number(sumPrice) == 0 ){
		isPay = 0;
		var loading = function(html,callback){
	        var dialog = null;
	        dialog = $.dialog({
	            id: 'loading',
	            fixed:true,
	            title:false,
	            lock:true,
	            padding:'10px 20px',
	            content: '<div class="loading-tip">'+html+'</div>'
	        });
	    }
		loading('数据正在同步！请耐心等待！<br>请勿刷新该页面，否则数据同步将会失败！');	
	}else{
		isPay = 1;
	}
	
	$.ajax({
		type:"POST",
		url:"/buy/checkToSubmit",
		async:false,
		data:{
			orderList:orderList,
			isPay:isPay,
			productId:productId 
		},
		success:function(data){
			if(data.isSuccess){
				if(data.orderMoney == 0.00){
					location.href="/buy/successOrder";
				}else{
					location.href="/buy/submitOrders";
				}
				
			}else {
				BSTYUN.dialog.warn("网络异常，请求失败！");
			}
		}
	})
}